package cn.wit.front.controller;

import cn.wit.front.condition.LoginCondition;
import cn.wit.front.condition.UserRegisterCondition;
import cn.wit.front.service.LoginService;
import cn.wit.utils.pojo.VisitCount;
import cn.wit.utils.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "登录相关")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("login")
    @ApiOperation("登陆")
    public Result login(@RequestBody LoginCondition condition) {
        return Result.ok(loginService.login(condition));
    }

    @PostMapping("logout")
    @ApiOperation("登出")
    public Result logout(String token) {
        loginService.logout(token);
        return Result.ok();
    }
    @PostMapping("register")
    @ApiOperation("注册")
    public Result register(@RequestBody UserRegisterCondition condition) {
        loginService.register(condition);
        return Result.ok();
    }


    @GetMapping("/visit-count")
    public List<VisitCount> getVisitCount() {
        List<VisitCount> visitCounts = new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // 创建格式化器，指定日期格式
        List<String> dateList = new ArrayList<>(); // 创建一个 ArrayList 容器，用于存储日期字符串
        for (int i = 0; i < 3; i++) {
            Calendar calendar = Calendar.getInstance(); // 获取当前时间的 Calendar 对象
            calendar.add(Calendar.DATE, -i); // 将时间向前推 i 天
            String dateStr = formatter.format(calendar.getTime()); // 将时间格式化为字符串
            Integer count=(Integer)redisTemplate.opsForValue().get(dateStr);
            if(count!=null){
                visitCounts.add(new VisitCount(dateStr,count));
            }else{
                visitCounts.add(new VisitCount(dateStr,0));
            }
        }
        return visitCounts;
    }

}
